﻿using JESAI.HttpRequestMonitor.Core.Abstracts;
using JESAI.HttpRequestMonitor.Storage.MySql;
using System;
using System.Runtime.CompilerServices;

namespace Microsoft.Extensions.DependencyInjection
{
    public static class ServiceBuildExtensions
    {
        public static IHttpRequestMonitorBuilder UseMySqlStorage(this IHttpRequestMonitorBuilder builder)
        {
            builder.Services.AddOptions();
            builder.Services.Configure<MySqlStorageOptions>(builder.Configuration.GetSection("Storage"));
            return builder.UseMySqlStorageService();
        }

        public static IHttpRequestMonitorBuilder UseMySqlStorage(this IHttpRequestMonitorBuilder builder, Action<MySqlStorageOptions> options)
        {
            builder.Services.AddOptions();
            builder.Services.Configure<MySqlStorageOptions>(options);

            return builder.UseMySqlStorageService();
        }

        internal static IHttpRequestMonitorBuilder UseMySqlStorageService(this IHttpRequestMonitorBuilder builder)
        {
            builder.Services.AddSingleton<IRequestMonitorStorage, MySqlStorage>();
            builder.Services.AddSingleton<MySqlConnectionFactory>();

            return builder;

        }
    }
}